<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<html>
<head>


<script type="text/javascript">
var listUrl="rest/menuList.do";  //列表展示url
$(document).ready(function () {
//     $("#submit_menu-search").click(function () {
//         $('#menu-dg').treegrid({ queryParams: form2Json("fms") });   //点击搜索
//     });

//     $("#reset_menu-search").click(function (){
//     	$("#menuname").val("");
//     });

    commonTreeGrid(); //加载树形表格
})//end ready


//树形grid
function commonTreeGrid(){
    $('#menu-dg').treegrid({
		//title:'菜单管理',
	    width:'100%',
// 	    height:document.body.clientHeight-165,
		collapsible:true, //是否可折叠的
		fitColumns:true,//自适应列宽
		singleSelect:false,//是否单选
		rownumbers:true, //行号
		pagination:false,//分页控件   .  注:如果使用分页，只给根节点分页！！！
		url:listUrl,
		queryParams:
		{
		   "menuname":  $("#menuname").val()
		},
		idField : 'id',
        treeField : 'menuname',
		showFooter:true,
		loadMsg:'数据加载中....',
		frozenColumns:[[
					    {field:'id',checkbox:true}
					 ]],
		columns: [
		            [
		                {field: 'menuname', title: '菜单名称', width: 100},
		                {field: 'menudesc', title: '菜单备注', width: 100},
		                {field: 'menucode', title: '菜单链接', width: 100}
		            ]
		        ],
		 onContextMenu: onContextMenu
       });
}


function onContextMenu(e,row){
	e.preventDefault();
	$(this).treegrid('select', row.id);
	$('#mm').menu('show',{
		left: e.pageX,
		top: e.pageY
	});
}

function newMenu() {
    var rows = $('#menu-dg').datagrid('getSelections');
    if (rows.length > 1) {
        $.messager.alert('操作提示', "最多选择一条父级菜单信息！");
        return;
    }
    var row = $('#menu-dg').datagrid('getSelected');

    var url = "menu/add.do";
    $.post(url, (row==null?"pid=0" : "pid=" + row.id),
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}

//编辑菜单
function editMenu() {
    var rows = $('#menu-dg').datagrid('getSelections');
    if (rows.length != 1) {
        $.messager.alert('操作提示', "请先选择一条要编辑的信息！");
        return;
    }
    var row = $('#menu-dg').datagrid('getSelected');
    var id=row.id;

    var url = "menu/edit.do";
    $.post(url, row,
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}

//删除菜单
function removeMenu() {
    var array = $('#menu-dg').datagrid('getSelections');
    var ids = new Array();
    for (var i = 0; i < array.length; i++) {//组成一个字符串，ID主键之间用逗号隔开
        ids[i] = array[i].id;
    }

    ids = ids.join(',');

    if (array != "") {
        $.messager.defaults = {ok: "确定", cancel: "取消"};
        $.messager.confirm('操作提示', '是否要删除该信息？', function (r) {
            if (r) {
            	$.post('rest/menuDel.do',
                    {  ids: ids}, function (response) {

                        if (response.success == "false") {
                            $.messager.alert('操作提示', "删除失败！", 'error');

                        } else if(response.success =="children"){ //说明该菜单还有子级菜单
                        	 $.messager.alert('操作提示', "该菜单还有子级菜单不能删除！", 'error');
                        }else {
                            $.messager.alert('操作提示', "删除成功！", 'info');
                            $('#menu-dg').treegrid('reload');
                        }
                 });
            }
        });
    } else {
        $.messager.alert('操作提示', "请先选择要删除的信息！");
    }
};



</script>
</head>
<body>
		<div class="linkbar"><a>系统</a>/<a>菜单管理</a></div>
		
		
		<div class="clear"></div>
		<div id="menu-dg" toolbar="#toolbar"></div>

<!--         <div data-options="region:'center',border:false"  style="overflow: hidden;"> -->
<!--             <table id="menu-dg"></table> -->
<!--         </div> -->


		<div id="toolbar">
			<shiro:hasPermission name="menu:add">  
			<a href="#" class="easyui-linkbutton" iconCls="icon-add"    plain="true"	onclick="newMenu()">添加菜单</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="menu:edit">
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit"   plain="true" onclick="editMenu()">编辑菜单</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="menu:remove">
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeMenu()">删除菜单</a>
			</shiro:hasPermission>
		</div>


</body>
</html>